import asyncio
from multimedia_tools.config import settings

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pathlib import Path

from multimedia_tools.routers import (
    router_pic,
    router_video,
    router_audio,
    router_modify,
    router_test,
)

app = FastAPI(
    title="多媒体生成工具",
    description="多媒体生成工具的 FastAPI 应用",
    version="0.0.1",
)


app.include_router(router_pic)
app.include_router(router_video)
app.include_router(router_audio)
app.include_router(router_modify)
app.include_router(router_test)

staticfiles = StaticFiles(directory=Path(__file__).parent / "static/")
app.mount("/static", staticfiles, name="static")  # index.html 里面需要引入这个静态目录


@app.middleware("http")
async def add_static_files(request: Request, call_next):
    print(f"请求路径: {request.url.path}")
    response = await call_next(request)
    return response


@app.get("/", response_class=FileResponse)
async def root():
    # 构建静态HTML文件的完整路径
    html_path = (
        Path(__file__).parent / "static" / "index.html"
    )  # 假设文件名为index.html

    if not html_path.exists():
        raise HTTPException(status_code=404, detail="首页文件未找到")

    # 返回HTML文件
    return FileResponse(html_path)


async def main():
    import uvicorn

    uvicorn.run(
        app="multimedia_tools.main:app",
        host="0.0.0.0",
        port=settings.APP_PORT,
        reload=settings.APP_AUTO_RELOAD,
    )


if __name__ == "__main__":
    asyncio.run(main())
